home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch7 / MFilter.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-30  |  21.9 KB  |  673 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmMFilter 
  4.    Caption         =   "MFilter []"
  5.    ClientHeight    =   7365
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   11610
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   7365
  11.    ScaleWidth      =   11610
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog dlgOpenFile 
  14.       Left            =   2760
  15.       Top             =   0
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.PictureBox picOriginal 
  21.       AutoSize        =   -1  'True
  22.       Height          =   7035
  23.       Left            =   120
  24.       Picture         =   "MFilter.frx":0000
  25.       ScaleHeight     =   465
  26.       ScaleMode       =   3  'Pixel
  27.       ScaleWidth      =   245
  28.       TabIndex        =   1
  29.       Top             =   240
  30.       Width           =   3735
  31.    End
  32.    Begin VB.PictureBox picResult 
  33.       Height          =   7035
  34.       Left            =   7800
  35.       ScaleHeight     =   465
  36.       ScaleMode       =   3  'Pixel
  37.       ScaleWidth      =   245
  38.       TabIndex        =   0
  39.       Top             =   240
  40.       Width           =   3735
  41.    End
  42.    Begin VB.PictureBox picMask 
  43.       AutoSize        =   -1  'True
  44.       Height          =   7035
  45.       Left            =   3960
  46.       Picture         =   "MFilter.frx":53922
  47.       ScaleHeight     =   465
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   245
  50.       TabIndex        =   3
  51.       Top             =   240
  52.       Width           =   3735
  53.    End
  54.    Begin VB.Label lblFilterType 
  55.       Alignment       =   2  'Center
  56.       Height          =   255
  57.       Left            =   7800
  58.       TabIndex        =   2
  59.       Top             =   0
  60.       Width           =   3735
  61.    End
  62.    Begin VB.Menu mnuFile 
  63.       Caption         =   "&File"
  64.       Begin VB.Menu mnuFileOpen 
  65.          Caption         =   "&Open..."
  66.          Shortcut        =   ^O
  67.       End
  68.       Begin VB.Menu mnuFileSaveAs 
  69.          Caption         =   "Save &As..."
  70.          Shortcut        =   ^A
  71.       End
  72.    End
  73.    Begin VB.Menu mnuFilter 
  74.       Caption         =   "Fil&ter"
  75.       Begin VB.Menu mnuFilterIdentity 
  76.          Caption         =   "&Identity"
  77.       End
  78.       Begin VB.Menu mnuFilterLowPass 
  79.          Caption         =   "&Low Pass"
  80.          Begin VB.Menu mnuLowPass 
  81.             Caption         =   "&3x3 Uniform"
  82.             Index           =   3
  83.          End
  84.          Begin VB.Menu mnuLowPass 
  85.             Caption         =   "&5x5 Uniform"
  86.             Index           =   5
  87.          End
  88.          Begin VB.Menu mnuLowPass 
  89.             Caption         =   "&7x7 Uniform"
  90.             Index           =   7
  91.          End
  92.          Begin VB.Menu mnuLowPassSep1 
  93.             Caption         =   "-"
  94.          End
  95.          Begin VB.Menu mnuFilterLowPassPeaked 
  96.             Caption         =   "3x3 Peaked"
  97.             Index           =   3
  98.          End
  99.          Begin VB.Menu mnuFilterLowPassPeaked 
  100.             Caption         =   "5x5 Peaked"
  101.             Index           =   5
  102.          End
  103.          Begin VB.Menu mnuFilterLowPassPeaked 
  104.             Caption         =   "7x7 Peaked"
  105.             Index           =   7
  106.          End
  107.          Begin VB.Menu mnuLowPassSep2 
  108.             Caption         =   "-"
  109.          End
  110.          Begin VB.Menu mnuLowPassStrongPeak 
  111.             Caption         =   "&Strongly Peaked"
  112.          End
  113.       End
  114.       Begin VB.Menu mnuFilterHighPass 
  115.          Caption         =   "&High Pass"
  116.          Begin VB.Menu mnuHighPassVeryWeak 
  117.             Caption         =   "3x3 Very Weak"
  118.          End
  119.          Begin VB.Menu mnuHighPassWeak 
  120.             Caption         =   "3x3 &Weak"
  121.          End
  122.          Begin VB.Menu mnuHighPassStrong 
  123.             Caption         =   "3x3 &Strong"
  124.          End
  125.          Begin VB.Menu mnuHighPassVeryStrong 
  126.             Caption         =   "3x3 &Very Strong"
  127.          End
  128.       End
  129.       Begin VB.Menu mnuPrewittGradient 
  130.          Caption         =   "&Prewitt Gradient Edge Detection"
  131.          Begin VB.Menu mnuPrewitt 
  132.             Caption         =   "NW to SE"
  133.             Index           =   0
  134.          End
  135.          Begin VB.Menu mnuPrewitt 
  136.             Caption         =   "N to S"
  137.             Index           =   1
  138.          End
  139.          Begin VB.Menu mnuPrewitt 
  140.             Caption         =   "NE to SW"
  141.             Index           =   2
  142.          End
  143.          Begin VB.Menu mnuPrewitt 
  144.             Caption         =   "E to W"
  145.             Index           =   3
  146.          End
  147.          Begin VB.Menu mnuPrewitt 
  148.             Caption         =   "SE to NW"
  149.             Index           =   4
  150.          End
  151.          Begin VB.Menu mnuPrewitt 
  152.             Caption         =   "S to N"
  153.             Index           =   5
  154.          End
  155.          Begin VB.Menu mnuPrewitt 
  156.             Caption         =   "SW to NE"
  157.             Index           =   6
  158.          End
  159.          Begin VB.Menu mnuPrewitt 
  160.             Caption         =   "W to E"
  161.             Index           =   7
  162.          End
  163.       End
  164.       Begin VB.Menu mnuLaplacianEdgeDetection 
  165.          Caption         =   "&Laplacian Edge Detection"
  166.          Begin VB.Menu mnuLaplacianWeak 
  167.             Caption         =   "&Weak"
  168.          End
  169.          Begin VB.Menu mnuLaplacianStrong 
  170.             Caption         =   "&Strong"
  171.          End
  172.          Begin VB.Menu mnuLaplacianVeryStrong 
  173.             Caption         =   "&Very Strong"
  174.          End
  175.       End
  176.       Begin VB.Menu mnuFilterSep 
  177.          Caption         =   "-"
  178.          Index           =   8
  179.       End
  180.       Begin VB.Menu mnuFilterShowFilter 
  181.          Caption         =   "&Show Filter"
  182.          Enabled         =   0   'False
  183.       End
  184.       Begin VB.Menu mnuFilterCustom 
  185.          Caption         =   "&Define Custom Filter"
  186.       End
  187.    End
  188. Attribute VB_Name = "frmMFilter"
  189. Attribute VB_GlobalNameSpace = False
  190. Attribute VB_Creatable = False
  191. Attribute VB_PredeclaredId = True
  192. Attribute VB_Exposed = False
  193. Option Explicit
  194. Private TheKernel() As Single
  195. ' Manage the mouse and apply the image.
  196. Private Sub ApplyTheFilter()
  197.     ' Do nothing if no picture is loaded.
  198.     If picOriginal.Picture = 0 Then Exit Sub
  199.     ' Do nothing if no filter is loaded.
  200.     If Len(lblFilterType.Caption) = 0 Then Exit Sub
  201.     Screen.MousePointer = vbHourglass
  202.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  203.         picResult.BackColor, BF
  204.     DoEvents
  205.     ' Apply the filter.
  206.     ApplyFilter TheKernel
  207.     Screen.MousePointer = vbDefault
  208. End Sub
  209. ' Arrange the controls.
  210. Private Sub ArrangeControls()
  211.     ' Position the result PictureBox.
  212.     picResult.Move _
  213.         picOriginal.Left + picOriginal.Width + 120, _
  214.         picOriginal.Top, _
  215.         picOriginal.Width, _
  216.         picOriginal.Height
  217.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  218.         picResult.BackColor, BF
  219.     lblFilterType.Move picResult.Left, _
  220.         0, picResult.Width
  221.     ' This makes the image resize itself to
  222.     ' fit the picture.
  223.     picResult.Picture = picResult.Image
  224.     ' Make the form big enough.
  225.     Width = picResult.Left + picResult.Width + _
  226.         Width - ScaleWidth + 120
  227.     Height = picResult.Top + picResult.Height + _
  228.         Height - ScaleHeight + 120
  229.     DoEvents
  230. End Sub
  231. ' Apply a filter to an image.
  232. Private Sub ApplyFilter(kernel() As Single)
  233. Dim bound As Integer
  234. Dim input_pixels() As RGBTriplet
  235. Dim mask_pixels() As RGBTriplet
  236. Dim result_pixels() As RGBTriplet
  237. Dim bits_per_pixel As Integer
  238. Dim X As Integer
  239. Dim Y As Integer
  240. Dim i As Integer
  241. Dim j As Integer
  242. Dim r As Integer
  243. Dim g As Integer
  244. Dim b As Integer
  245. Dim new_fraction As Single
  246.     ' Get the kernel's bounds.
  247.     bound = UBound(kernel, 1)
  248.     ' Get the pixels from picOriginal.
  249.     GetBitmapPixels picOriginal, input_pixels, bits_per_pixel
  250.     ' Get the mask pixels.
  251.     GetBitmapPixels picMask, mask_pixels, bits_per_pixel
  252.     ' Allocate space for the result pixels.
  253.     ReDim result_pixels( _
  254.         LBound(input_pixels, 1) To UBound(input_pixels, 1), _
  255.         LBound(input_pixels, 2) To UBound(input_pixels, 2))
  256.     ' Set the pixel colors. Note that we
  257.     ' must skip the edges because some of
  258.     ' the kernel values would correspond
  259.     ' to pixels off the image.
  260.     For Y = bound To picOriginal.ScaleHeight - 1 - bound
  261.         For X = bound To picOriginal.ScaleWidth - 1 - bound
  262.             ' See what fraction of the result
  263.             ' should be due to the new value.
  264.             new_fraction = (255 - mask_pixels(X, Y).rgbRed) / 255
  265.             If new_fraction < 0.001 Then
  266.                 ' Don't bother to apply the filter.
  267.                 ' Set the output pixel equal to
  268.                 ' the input pixel.
  269.                 result_pixels(X, Y) = input_pixels(X, Y)
  270.             Else
  271.                 ' Start with no color.
  272.                 r = 0
  273.                 g = 0
  274.                 b = 0
  275.                 ' Apply the kernel values to
  276.                 ' the nearby pixels.
  277.                 For i = -bound To bound
  278.                     For j = -bound To bound
  279.                         With input_pixels(X + i, Y + j)
  280.                             r = r + .rgbRed * kernel(j, i)
  281.                             g = g + .rgbGreen * kernel(j, i)
  282.                             b = b + .rgbBlue * kernel(j, i)
  283.                         End With
  284.                     Next j
  285.                 Next i
  286.                 ' Make sure the values are
  287.                 ' between 0 and 255.
  288.                 If r < 0 Then r = 0
  289.                 If r > 255 Then r = 255
  290.                 If g < 0 Then g = 0
  291.                 If g > 255 Then g = 255
  292.                 If b < 0 Then b = 0
  293.                 If b > 255 Then b = 255
  294.                 ' Set the output pixel value.
  295.                 With result_pixels(X, Y)
  296.                     .rgbRed = new_fraction * r + (1 - new_fraction) * input_pixels(X, Y).rgbRed
  297.                     .rgbGreen = new_fraction * g + (1 - new_fraction) * input_pixels(X, Y).rgbGreen
  298.                     .rgbBlue = new_fraction * b + (1 - new_fraction) * input_pixels(X, Y).rgbBlue
  299.                 End With
  300.             End If
  301.         Next X
  302.     Next Y
  303.     ' Set picResult's pixels.
  304.     SetBitmapPixels picResult, bits_per_pixel, result_pixels
  305.     picResult.Picture = picResult.Image
  306. End Sub
  307. ' Copy kernel entries from a variant array of
  308. ' variant arrays into a normal array.
  309. Private Sub VariantToArray(ByVal var As Variant, ByRef arr() As Single)
  310. Dim bound As Integer
  311. Dim i As Integer
  312. Dim j As Integer
  313.     bound = UBound(var) \ 2
  314.     ReDim arr(-bound To bound, -bound To bound)
  315.     For i = -bound To bound
  316.         For j = -bound To bound
  317.             arr(i, j) = var(i + bound)(j + bound)
  318.         Next j
  319.     Next i
  320. End Sub
  321. ' Start in the current directory.
  322. Private Sub Form_Load()
  323.     picOriginal.AutoSize = True
  324.     picOriginal.ScaleMode = vbPixels
  325.     picOriginal.AutoRedraw = True
  326.     picMask.AutoSize = True
  327.     picMask.ScaleMode = vbPixels
  328.     picMask.AutoRedraw = True
  329.     picResult.ScaleMode = vbPixels
  330.     picResult.AutoRedraw = True
  331.     dlgOpenFile.CancelError = True
  332.     dlgOpenFile.InitDir = App.Path
  333.     dlgOpenFile.Filter = _
  334.         "Bitmaps (*.bmp)|*.bmp|" & _
  335.         "GIFs (*.gif)|*.gif|" & _
  336.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  337.         "Icons (*.ico)|*.ico|" & _
  338.         "Cursors (*.cur)|*.cur|" & _
  339.         "Run-Length Encoded (*.rle)|*.rle|" & _
  340.         "Metafiles (*.wmf)|*.wmf|" & _
  341.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  342.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  343.         "All Files (*.*)|*.*"
  344. End Sub
  345. ' Load the indicated file.
  346. Private Sub mnuFileOpen_Click()
  347. Dim file_name As String
  348.     ' Let the user select a file.
  349.     On Error Resume Next
  350.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  351.     dlgOpenFile.ShowOpen
  352.     If Err.Number = cdlCancel Then
  353.         Exit Sub
  354.     ElseIf Err.Number <> 0 Then
  355.         Beep
  356.         MsgBox "Error selecting file.", , vbExclamation
  357.         Exit Sub
  358.     End If
  359.     On Error GoTo 0
  360.     Screen.MousePointer = vbHourglass
  361.     DoEvents
  362.     file_name = Trim$(dlgOpenFile.FileName)
  363.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  364.         - Len(dlgOpenFile.FileTitle) - 1)
  365.     Caption = "MFilter [" & dlgOpenFile.FileTitle & "]"
  366.     ' Open the original file.
  367.     On Error GoTo LoadError
  368.     picOriginal.Picture = LoadPicture(file_name)
  369.     On Error GoTo 0
  370.     ' Make picResult the same size and position it.
  371.     ArrangeControls
  372.     ' Apply the filter.
  373.     ApplyTheFilter
  374.     Screen.MousePointer = vbDefault
  375.     Exit Sub
  376. LoadError:
  377.     Screen.MousePointer = vbDefault
  378.     MsgBox "Error " & Format$(Err.Number) & _
  379.         " opening file '" & file_name & "'" & vbCrLf & _
  380.         Err.Description
  381. End Sub
  382. ' Save the transformed image.
  383. Private Sub mnuFileSaveAs_Click()
  384. Dim file_name As String
  385.     ' Let the user select a file.
  386.     On Error Resume Next
  387.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  388.     dlgOpenFile.ShowSave
  389.     If Err.Number = cdlCancel Then
  390.         Exit Sub
  391.     ElseIf Err.Number <> 0 Then
  392.         Beep
  393.         MsgBox "Error selecting file.", , vbExclamation
  394.         Exit Sub
  395.     End If
  396.     On Error GoTo 0
  397.     Screen.MousePointer = vbHourglass
  398.     DoEvents
  399.     file_name = Trim$(dlgOpenFile.FileName)
  400.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  401.         - Len(dlgOpenFile.FileTitle) - 1)
  402.     Caption = "MFilter [" & dlgOpenFile.FileTitle & "]"
  403.     ' Save the transformed image into the file.
  404.     On Error GoTo SaveError
  405.     SavePicture picResult.Picture, file_name
  406.     On Error GoTo 0
  407.     Screen.MousePointer = vbDefault
  408.     Exit Sub
  409. SaveError:
  410.     Screen.MousePointer = vbDefault
  411.     MsgBox "Error " & Format$(Err.Number) & _
  412.         " saving file '" & file_name & "'" & vbCrLf & _
  413.         Err.Description
  414. End Sub
  415. ' Let the user define a custom filter.
  416. Private Sub mnuFilterCustom_Click()
  417. Dim bound As Integer
  418. Dim i As Integer
  419. Dim j As Integer
  420. Dim idx As Integer
  421.     frmCustom.Show vbModal
  422.     If Not frmCustom.Canceled Then
  423.         bound = frmCustom.CustomBound
  424.         ReDim TheKernel(-bound To bound, -bound To bound)
  425.         idx = 0
  426.         For i = -bound To bound
  427.             For j = -bound To bound
  428.                 TheKernel(i, j) = CSng(frmCustom.txtCoefficient(idx))
  429.                 idx = idx + 1
  430.             Next j
  431.         Next i
  432.         mnuFilterShowFilter.Enabled = True
  433.         lblFilterType.Caption = "Custom " & _
  434.             Format$(bound) & "x" & Format$(bound)
  435.     End If
  436.     Unload frmCustom
  437. End Sub
  438. Private Sub mnuFilterIdentity_Click()
  439.     ' Create an identity kernel.
  440.     ReDim TheKernel(0 To 0, 0 To 0)
  441.     TheKernel(0, 0) = 1#
  442.     ' Prepare some controls.
  443.     mnuFilterShowFilter.Enabled = True
  444.     lblFilterType.Caption = "Identity"
  445.     ' Apply the filter.
  446.     ApplyTheFilter
  447. End Sub
  448. ' Display the filter coefficients.
  449. Private Sub mnuFilterShowFilter_Click()
  450.     frmShowFilter.PrepareForm TheKernel
  451.     frmShowFilter.Show vbModal
  452. End Sub
  453. ' Apply a strong high pass filter.
  454. Private Sub mnuHighPassStrong_Click()
  455.     ' Build the kernel.
  456.     VariantToArray Array( _
  457.         Array(0, -1, 0), _
  458.         Array(-1, 5, -1), _
  459.         Array(0, -1, 0)), _
  460.         TheKernel
  461.     ' Prepare some controls.
  462.     mnuFilterShowFilter.Enabled = True
  463.     lblFilterType.Caption = "Strong High Pass 3x3"
  464.     ApplyTheFilter
  465. End Sub
  466. ' Apply a very strong high pass filter.
  467. Private Sub mnuHighPassVeryStrong_Click()
  468.     ' Build the kernel.
  469.     VariantToArray Array( _
  470.         Array(-1, -1, -1), _
  471.         Array(-1, 9, -1), _
  472.         Array(-1, -1, -1)), _
  473.         TheKernel
  474.     ' Prepare some controls.
  475.     mnuFilterShowFilter.Enabled = True
  476.     lblFilterType.Caption = "Very Strong High Pass 3x3"
  477.     ApplyTheFilter
  478. End Sub
  479. ' Apply a very weak high pass filter.
  480. Private Sub mnuHighPassVeryWeak_Click()
  481.     ' Build the kernel.
  482.     VariantToArray Array( _
  483.         Array(-1 / 12, -1 / 12, -1 / 12), _
  484.         Array(-1 / 12, 20 / 12, -1 / 12), _
  485.         Array(-1 / 12, -1 / 12, -1 / 12)), _
  486.         TheKernel
  487.     ' Prepare some controls.
  488.     mnuFilterShowFilter.Enabled = True
  489.     lblFilterType.Caption = "Weak High Pass 3x3"
  490.     ApplyTheFilter
  491. End Sub
  492. ' Apply a weak high pass filter.
  493. Private Sub mnuHighPassWeak_Click()
  494.     ' Build the kernel.
  495.     VariantToArray Array( _
  496.         Array(-1 / 4, -1 / 4, -1 / 4), _
  497.         Array(-1 / 4, 12 / 4, -1 / 4), _
  498.         Array(-1 / 4, -1 / 4, -1 / 4)), _
  499.         TheKernel
  500.     ' Prepare some controls.
  501.     mnuFilterShowFilter.Enabled = True
  502.     lblFilterType.Caption = "Weak High Pass 3x3"
  503.     ApplyTheFilter
  504. End Sub
  505. ' Apply a weak Laplacian edge detection filter.
  506. Private Sub mnuLaplacianWeak_Click()
  507.     ' Build the kernel.
  508.     VariantToArray Array( _
  509.         Array(0, -1, 0), _
  510.         Array(-1, 4, -1), _
  511.         Array(0, -1, 0)), _
  512.         TheKernel
  513.     ' Prepare some controls.
  514.     mnuFilterShowFilter.Enabled = True
  515.     lblFilterType.Caption = "Weak Laplacian 3x3"
  516.     ApplyTheFilter
  517. End Sub
  518. ' Apply a strong Laplacian edge detection filter.
  519. Private Sub mnuLaplacianStrong_Click()
  520.     ' Build the kernel.
  521.     VariantToArray Array( _
  522.         Array(-1, -1, -1), _
  523.         Array(-1, 8, -1), _
  524.         Array(-1, -1, -1)), _
  525.         TheKernel
  526.     ' Prepare some controls.
  527.     mnuFilterShowFilter.Enabled = True
  528.     lblFilterType.Caption = "Strong Laplacian 3x3"
  529.     ApplyTheFilter
  530. End Sub
  531. ' Apply a very strong Laplacian edge detection filter.
  532. Private Sub mnuLaplacianVeryStrong_Click()
  533.     ' Build the kernel.
  534.     VariantToArray Array( _
  535.         Array(-1, -2, -1), _
  536.         Array(-2, 12, -2), _
  537.         Array(-1, -2, -1)), _
  538.         TheKernel
  539.     ' Prepare some controls.
  540.     mnuFilterShowFilter.Enabled = True
  541.     lblFilterType.Caption = "Very Strong Laplacian 3x3"
  542.     ApplyTheFilter
  543. End Sub
  544. ' Apply a low pass filter.
  545. Private Sub mnuLowPass_Click(Index As Integer)
  546. Dim bound As Integer
  547. Dim i As Integer
  548. Dim j As Integer
  549.     ' Build the kernel.
  550.     bound = (Index - 1) \ 2
  551.     ReDim TheKernel(-bound To bound, -bound To bound)
  552.     For i = -bound To bound
  553.         For j = -bound To bound
  554.             TheKernel(i, j) = 1 / (Index * Index)
  555.         Next j
  556.     Next i
  557.     ' Prepare some controls.
  558.     mnuFilterShowFilter.Enabled = True
  559.     lblFilterType.Caption = "Identity"
  560.     ' Apply the filter.
  561.     lblFilterType.Caption = "Low Pass " & _
  562.         Format$(Index) & "x" & _
  563.         Format$(Index)
  564.     ApplyTheFilter
  565. End Sub
  566. ' Apply a peaked low pass filter.
  567. Private Sub mnuFilterLowPassPeaked_Click(Index As Integer)
  568. Dim bound As Integer
  569. Dim i As Integer
  570. Dim j As Integer
  571. Dim total_weight As Integer
  572.     ' Build the kernel.
  573.     bound = (Index - 1) \ 2
  574.     ReDim TheKernel(-bound To bound, -bound To bound)
  575.     For i = -bound To bound
  576.         For j = -bound To bound
  577.             TheKernel(i, j) = 2 * bound + 1 - Abs(i) - Abs(j)
  578.             total_weight = total_weight + TheKernel(i, j)
  579.         Next j
  580.     Next i
  581.     ' Adjust the kernel so the sum of the
  582.     ' coefficients is 1.
  583.     For i = -bound To bound
  584.         For j = -bound To bound
  585.             TheKernel(i, j) = TheKernel(i, j) / total_weight
  586.         Next j
  587.     Next i
  588.     ' Prepare some controls.
  589.     mnuFilterShowFilter.Enabled = True
  590.     lblFilterType.Caption = "Low Pass Peaked " & _
  591.         Format$(Index) & "x" & _
  592.         Format$(Index)
  593.     ApplyTheFilter
  594. End Sub
  595. ' Apply a stongly peaked low pass filter.
  596. Private Sub mnuLowPassStrongPeak_Click()
  597. Dim i As Integer
  598. Dim j As Integer
  599.     ' Build the kernel.
  600.     ReDim TheKernel(-1 To 1, -1 To 1)
  601.     For i = -1 To 1
  602.         For j = -1 To 1
  603.             TheKernel(i, j) = 1 / 20
  604.         Next j
  605.     Next i
  606.     TheKernel(0, 0) = 12 / 20
  607.     ' Prepare some controls.
  608.     mnuFilterShowFilter.Enabled = True
  609.     lblFilterType.Caption = "Strongly Peaked 3x3"
  610.     ApplyTheFilter
  611. End Sub
  612. ' Apply a Prewitt edge detector.
  613. Private Sub mnuPrewitt_Click(Index As Integer)
  614. Dim i As Integer
  615. Dim j As Integer
  616.     ' Build the kernel.
  617.     Select Case Index
  618.         Case 0  ' NW to SE
  619.             VariantToArray Array( _
  620.                 Array(1, 1, 1), _
  621.                 Array(1, -2, -1), _
  622.                 Array(1, -1, -1)), _
  623.                 TheKernel
  624.         Case 1  ' N to S
  625.             VariantToArray Array( _
  626.                 Array(1, 1, 1), _
  627.                 Array(1, -2, 1), _
  628.                 Array(-1, -1, -1)), _
  629.                 TheKernel
  630.         Case 2  ' NE to SW
  631.             VariantToArray Array( _
  632.                 Array(1, 1, 1), _
  633.                 Array(-1, -2, 1), _
  634.                 Array(-1, -1, 1)), _
  635.                 TheKernel
  636.         Case 3  ' E to W
  637.             VariantToArray Array( _
  638.                 Array(-1, 1, 1), _
  639.                 Array(-1, -2, 1), _
  640.                 Array(-1, 1, 1)), _
  641.                 TheKernel
  642.         Case 4  ' SE to NW
  643.             VariantToArray Array( _
  644.                 Array(-1, -1, 1), _
  645.                 Array(-1, -2, 1), _
  646.                 Array(1, 1, 1)), _
  647.                 TheKernel
  648.         Case 5  ' S to N
  649.             VariantToArray Array( _
  650.                 Array(-1, -1, -1), _
  651.                 Array(1, -2, 1), _
  652.                 Array(1, 1, 1)), _
  653.                 TheKernel
  654.         Case 6  ' SW to NE
  655.             VariantToArray Array( _
  656.                 Array(1, -1, -1), _
  657.                 Array(1, -2, -1), _
  658.                 Array(1, 1, 1)), _
  659.                 TheKernel
  660.         Case 7  ' W to E
  661.             VariantToArray Array( _
  662.                 Array(1, 1, -1), _
  663.                 Array(1, -2, -1), _
  664.                 Array(1, 1, -1)), _
  665.                 TheKernel
  666.     End Select
  667.     ' Prepare some controls.
  668.     mnuFilterShowFilter.Enabled = True
  669.     lblFilterType.Caption = "Prewitt " & _
  670.         mnuPrewitt(Index).Caption
  671.     ApplyTheFilter
  672. End Sub
  673.